package com.project.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

public class Verifier {

    public static ResultTokenVo verifierJwt(String token){
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(CreateToken.TOKEN_SECRET);
        String info="";
        JWTVerifier verifier = JWT.require(algorithm).build();
        ResultTokenVo vo=new ResultTokenVo();
        try {
            DecodedJWT jwt = verifier.verify(token); //认证token
            info=jwt.getClaim("info").asString();//取token中的信息
            vo.setTag(1);
            vo.setJson(info);
        }catch (Exception e){
            vo.setTag(0);
        }
        return vo;
    }

    //解密
    public static void main(String[] args) {
        Algorithm algorithm = Algorithm.HMAC256(CreateToken.TOKEN_SECRET);
        JWTVerifier verifier = JWT.require(algorithm).build();

        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqc29uIjoie1wiYWdlXCI6MjAsXCJ1c2VyTmFtZVwiOlwi6LW15LqRXCJ9IiwiZXhwIjoxNjkwMjcyMTE5fQ.lxGDAbcvSgaZIwUEPu1Sjq245p1XrDoURp11-hxX-9g";
        try {

       DecodedJWT jwt =verifier.verify(token);
       //去json
          String json = jwt.getClaim("json").asString();
            System.out.println(json);

        }catch (Exception e){
            System.out.println("验证失败");
        }

    }
}